package com.cwd.arm.domain.repository.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.cwd.arm.common.enums.ErrorEnum;
import com.cwd.arm.common.exception.ArmException;
import com.cwd.arm.domain.entity.UserEntity;
import com.cwd.arm.domain.repository.UserRepository;
import com.cwd.arm.infrastructure.dao.UserDao;
import com.cwd.arm.infrastructure.dao.UserRoleDao;
import com.cwd.arm.infrastructure.po.UserPo;
import com.cwd.arm.infrastructure.po.UserRolePo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * Created by yan on 21-11-14.
 */
@Repository
public class UserRepositoryImpl implements UserRepository {

    @Autowired
    private UserDao dao;

    @Autowired
    private UserRoleDao userRoleDao;

    @Override
    public void save(UserEntity entity) {
        if(entity.getPhone()!=null){
            UserPo userPo = dao.getByPhone(entity.getPhone());
            if(userPo !=null ){
                throw new ArmException(ErrorEnum.VAILD_ERROR,"已存在该手机号的用户");
            }
            userPo = dao.getByUsername(entity.getUsername());
            if(userPo !=null ){
                throw new ArmException(ErrorEnum.VAILD_ERROR,"已存在该帐号的用户");
            }
        }
        entity.initPassword();
        UserPo po = convertToUserPo(entity);
        dao.save(po);
        if(entity.needUpdateRole()){
            addRole(entity, po);
        }
    }

    @Override
    public void update(UserEntity entity) {
        entity.adminVal();
        if(StringUtils.isNotEmpty(entity.getPassword())){
            entity.passwordVal();
            entity.initPassword();
        }
        UserPo po = convertToUserPo(entity);
        dao.updateById(po);
        if (entity.needUpdateRole()) {
            //目前只有一种角色
            userRoleDao.removeByUserId(entity.getId());
            UserRolePo userRolePo = new UserRolePo();
            userRolePo.setRoleId(entity.getRoleId());
            userRolePo.setUserId(entity.getId());
            userRoleDao.save(userRolePo);
        }
    }

    @Override
    public void del(Long id) {
        UserEntity userEntity = buildUserEntity(id);
        if (userEntity.canDel()) {
            dao.removeById(id);
            userRoleDao.removeByUserId(id);
        }
    }

    private void addRole(UserEntity entity, UserPo po) {
        UserRolePo userRolePo = new UserRolePo();
        userRolePo.setUserId(po.getId());
        userRolePo.setRoleId(entity.getRoleId());
        userRoleDao.save(userRolePo);
    }


    private UserEntity buildUserEntity(Long id) {
        UserEntity userEntity = new UserEntity();
        UserPo userPo = dao.getById(id);
        BeanUtils.copyProperties(userPo, userEntity);
        return userEntity;
    }

    private UserPo convertToUserPo(UserEntity entity) {
        UserPo userPo = new UserPo();
        BeanUtils.copyProperties(entity, userPo);
        return userPo;
    }

}
